Program a Concurrent Traffic Simulation

FP.1 Create a TrafficLight class

Criteria Meet Specification

The TrafficLight class is defined

A TrafficLight class is is defined which is a child class of TrafficObject.

The TrafficLight class methods are completed

The class shall have the public methods void waitForGreen() and void simulate() as well as TrafficLightPhase getCurrentPhase(), where TrafficLightPhase is an enum that can be either red or green.

Also, there should be a private method void cycleThroughPhases() and a private member _currentPhase which can take red or green as its value.

FP.2: Implement a cycleThroughPhases method

Criteria Meet Specification

The cycleThroughPhases() method is implemented.

Implement the function with an infinite loop that measures the time between two loop cycles and toggles the current phase of the traffic light between red and green.

The cycle duration should be a random value between 4 and 6 seconds, and the while-loop should use std::this_thread::sleep_for to wait 1ms between two cycles.

The cycleThroughPhases() method is started correctly.

The private cycleThroughPhases() method should be started in a thread when the public method simulate is called. To do this, a thread queue should be used in the base class.

FP.3 Define class MessageQueue

Criteria Meet Specification

A MessageQueue class is defined

A MessageQueue class is defined in the header of class TrafficLight which has the public methods send and receive.

The MessageQueue class methods and members are declared correctly

send should take an rvalue reference of type TrafficLightPhase whereas receive should return this type.

Also, the MessageQueue class should define a std::dequeue called _queue, which stores objects of type TrafficLightPhase.

Also, there should be a std::condition_variable as well as an std::mutex as private members.

FP.4 Implement the method send

Criteria Meet Specification

The method send is correctly implemented

The method send should use the mechanisms std::lock_guard<std::mutex> as well as _condition.notify_one() to add a new message to the queue and afterwards send a notification.


In the class TrafficLight, a private member of type MessageQueue should be created and used within the infinite loop to push each new TrafficLightPhase into it by calling send in conjunction with move semantics.

FP.5 Implement the methods receive and waitForGreen

Criteria Meet Specification

The method receive is correctly implemented

The method receive should use std::unique_lock<std::mutex> and
_condition.wait() to wait for and receive new messages and pull them from the queue using move semantics. The received object should then be returned by the receive function.

The method waitForGreen is correctly implemented

The method waitForGreen is completed, in which an infinite while loop runs and repeatedly calls the receive function on the message queue. Once it receives TrafficLightPhase::green, the method returns.

FP.6 Implement message exchange

Criteria Meet Specification

The message exchange is correctly implemented

In class Intersection, a private member _trafficLight of type TrafficLight should exist.

The method Intersection::simulate(), should start the simulation of
_trafficLight.

The method Intersection::addVehicleToQueue, should use the methods TrafficLight::getCurrentPhase and TrafficLight::waitForGreen to block
the execution until the traffic light turns green.